﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;
using School.Entities;
using School.Common;

namespace School.Pages.Testing
{
    public partial class ManageStudents : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LstCampuses.DataSource = Structure.GetCampuses();
                LstCampuses.DataTextField = "Name";
                LstCampuses.DataValueField = "ID";
                LstCampuses.DataBind();

                LstCertificates.DataSource = Structure.GetCertificatesByCampus(long.Parse(LstCampuses.SelectedValue));
                LstCertificates.DataValueField = "Id";
                LstCertificates.DataTextField = "Name";
                LstCertificates.DataBind();                

                LstCertificates.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الشهادة" });
                LstStages.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر المرحلة" });
                LstGrades.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الصف" });                
            }
        }

        protected void LstCampuses_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstCertificates.DataSource = Structure.GetCertificatesByCampus(long.Parse(LstCampuses.SelectedValue));
            LstCertificates.DataValueField = "Id";
            LstCertificates.DataTextField = "Name";
            LstCertificates.DataBind();

            LstCertificates.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الشهادة" });
        }

        protected void LstCertificates_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstStages.DataSource = Structure.GetStagesByCertificate(long.Parse(LstCertificates.SelectedValue));
            LstStages.DataValueField = "Id";
            LstStages.DataTextField = "Name";
            LstStages.DataBind();
            LstStages.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر المرحلة" });            
        }

        protected void LstStages_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstGrades.DataSource = Structure.GetGradesByStage(long.Parse(LstStages.SelectedValue));
            LstGrades.DataValueField = "Id";
            LstGrades.DataTextField = "Name";
            LstGrades.DataBind();
            LstGrades.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الصف" });
        }

        protected void LstGrades_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridView1.DataSource = Students.GetStudentsByGrade(long.Parse(LstGrades.SelectedValue)).Select(x => new { x.Id, Name = x.Ar_FirstName + " " + x.Ar_SecondName + " " + x.Ar_ThirdNmae + " " + x.Ar_FourthName });
            GridView1.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SchoolEntities db = new SchoolEntities();
            result.InnerHtml = "";

            foreach(GridViewRow row in GridView1.Rows)
            {
                long id = long.Parse(((Label)row.FindControl("LblId")).Text);
                bool selected = ((CheckBox)row.FindControl("ChkStudent")).Checked;

                if (selected)
                {
                    var student = db.TE_Student.Single(x => x.Id == id);

                    result.InnerHtml += student.Id.ToString() + ",";

                    /*try
                    {
                        db.DeleteObject(student.ACC_Student_Balance);
                        db.SaveChanges();
                    }
                    catch
                    {
                    }

                    try
                    {
                        db.DeleteObject(student);
                        db.SaveChanges();
                    }
                    catch
                    {
                    }*/
                }
            }
            
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            SchoolEntities db = new SchoolEntities();
            result.InnerHtml = "";

            foreach (GridViewRow row in GridView1.Rows)
            {
                long id = long.Parse(((Label)row.FindControl("LblId")).Text);
                bool selected = ((CheckBox)row.FindControl("ChkStudent")).Checked;

                if (selected)
                {
                    var student = db.TE_Student.Single(x => x.Id == id);

                    var grade = db.ORG_Grade_To_Grade.Single(x => x.Grade_Id2 == student.Grade_ID).Grade_Id;

                    student.Grade_ID = grade;
                    db.SaveChanges();
                }
            }
        }
    }
}